package com.ruoyi.web.controller.system;

import com.ruoyi.framework.web.service.HandlePasswordService;
import com.ruoyi.websocket.WebSocketManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.RegisterBody;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.SysRegisterService;
import com.ruoyi.system.service.ISysConfigService;

/**
 * 注册验证
 * 
 * @author ruoyi
 */
@RestController
public class SysRegisterController extends BaseController
{
    @Autowired
    private SysRegisterService registerService;

    @Autowired
    private ISysConfigService configService;
    @Autowired
    private HandlePasswordService handlePasswordService;

    @PostMapping("/register")
    public AjaxResult register(@RequestBody RegisterBody user)
    {
        if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser"))))
        {
            return error("当前系统没有开启注册功能！");
        }
        String msg = registerService.register(user);
        return StringUtils.isEmpty(msg) ? success() : error(msg);
    }

    @PostMapping("/register-h5")
    public AjaxResult registerH5(@RequestBody RegisterBody user)
    {
        if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser"))))
        {
            return error("当前系统没有开启注册功能！");
        }
        String msg = registerService.registerNew(user);
        return StringUtils.isEmpty(msg) ? success() : error(msg);
    }

    @GetMapping("/initAllUserPwd")
    public String setPwd(@RequestParam("pwd") String pwd) {
        if(!"admin-init-pwd".equals(pwd)) {
            return "密码错误";
        }
        handlePasswordService.buildImportedUserPwd();
        return "初始化成功";
    }

    @GetMapping("/testForkJoin")
    public String setPwd2() {
        handlePasswordService.buildImportedUserPwd2();
        return "初始化成功";
    }

    @GetMapping("/websocket/serverSendText")
    public String serverSendText() {
        WebSocketManager.sendMessageToAllKey("message");
        WebSocketManager.sendMessageToKey("285415-test", "单独信息");
        return "发送成功";
    }

}
